package com.wangsd.web.sys.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wangsd.common.basicbean.MyController;
import com.wangsd.common.basicbean.Result;
import com.wangsd.web.sys.entity.SysCompany;
import com.wangsd.web.sys.entity.SysUser;
import com.wangsd.web.sys.service.SysCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "服务商管理")
@RestController
@RequestMapping("/sys/company")
@Slf4j
public class SysCompanyController extends MyController {
    @Resource
    private SysCompanyService service;

    @ApiOperation(value = "分页查询服务商列表")
    @PostMapping("/selectPage")
    public Object selectPage(@ApiParam(value = "当前第几页", required = true) @RequestParam("pageNum") Integer pageNum,
                             @ApiParam(value = "每页结果集大小", required = true) @RequestParam("pageSize") Integer pageSize,
                             SysCompany queryObj) {
        log.debug("分页查询服务商列表-controller");
        SysUser loginUser = getUserInfo();
        Page page = PageHelper.startPage(pageNum, pageSize);
        List<SysCompany> list = service.selectPage(queryObj, loginUser.getCode());
        return Result.success(getPageInfo(page, list));
    }


    @ApiOperation(value = "删除服务商")
    @GetMapping("/{id}/delete")
    public Result delete(@ApiParam(value = "菜单id", required = true) @PathVariable() Long id) {
        log.debug("删除服务商或代理商-controller");
        service.delete(id);
        return Result.success();
    }


    @ApiOperation(value = "新增或者修改服务商")
    @PostMapping("/saveOrUpdate")
    public Object saveOrUpdate(@RequestBody SysCompany entity) {
        log.debug("新增或者修改服务商-controller");
        service.saveOrUpdate(entity);
        return Result.success();
    }

    @ApiOperation(value = "查询所有服务商（下拉列表）")
    @PostMapping("/selectAllCompany")
    public Result selectAllCompany(@ApiParam(value = "类型") @RequestParam(value = "type", required = false) Integer type) {
        log.debug("查询所有服务商（下拉列表）-controller");
        SysUser loginUser = getUserInfo();
        List<SysCompany> list = service.selectAllCompany(loginUser.getCode(), type);
        return Result.success(list);
    }

    @ApiOperation(value = "查询树形结构")
    @PostMapping("/selectAgentTree")
    public Object selectAgentTree(boolean isCode) {
        SysUser loginUser = getUserInfo();
        List list = service.selectAgentTree(loginUser.getCode());
        return Result.success(list);
    }

    @ApiOperation(value = "获取可用金额")
    @GetMapping("/{id}/getUseableAmount")
    public Result getUseableAmount(@ApiParam(value = "客户(商圈)id", required = true) @PathVariable Long id) {
        log.debug("刷新api秘钥-controller");
        SysCompany company = service.selectById(id);
        Map map = new HashMap();
        map.put("useableAmount", company.getProfitAmount() * 0.01);
        map.put("settleAmount", company.getProfitAmount() * 0.01);
        return Result.success(map);
    }

    @ApiOperation(value = "申请提现")
    @PostMapping("/createCustomerFlow")
    public Result createCustomerFlow(@ApiParam(value = "客户(商圈)id", required = true) @RequestParam Long id,
                                     @ApiParam(value = "提现金额", required = true) @RequestParam Integer settleAmount) {
        log.debug("申请提现-controller");
        int amount = settleAmount * 100;
        service.createCustomerFlow(id, amount);
        return Result.success();
    }
}
